package com.test.ssm.article.service.impl;

import com.test.ssm.article.dao.ArticleMapper;
import com.test.ssm.article.entity.pojo.Article;
import com.test.ssm.article.entity.vo.ResponseMessage;
import com.test.ssm.article.service.ArticleService;
import com.test.ssm.article.util.helper.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

//    @Override
//    public List<Article> getAll(Byte newsState,String newsTitle) {
//
//        return articleMapper.getAll(newsState,newsTitle);
//    }

    @Override
    public MyPage<Article> getAll(Byte newsState, String newsTitle, Integer pageNum, Integer pageSize) {
        List<Article> list =  articleMapper.getAll(newsState,newsTitle,pageNum,pageSize);
        Long count=  articleMapper.countAllByLike(Integer.valueOf(newsState),newsTitle);
        MyPage<Article> page = new MyPage<>(pageNum,pageSize,count,5,list);
        return page;
    }

    @Override
    public Article checkIsExist(String newsTitle) {
        return articleMapper.selectOneByTitle(newsTitle);
    }

    @Override
    public ResponseMessage add(Article article, MultipartFile multipartFile, HttpServletRequest request) {
        ResponseMessage responseMessage = new ResponseMessage();
        if (!multipartFile.isEmpty()) {
            String filename = multipartFile.getOriginalFilename();
            String suffix = filename.substring(filename.indexOf("."));
            if (multipartFile.getSize() < 5 * 1024 * 1024) {
                List<String> list = Arrays.asList(".jpg", ".jpeg", ".tif", ".png");
                if (list.contains(suffix)) {
                    String realPath = request.getServletContext().getRealPath("/upload/");
                    String paths = UUID.randomUUID().toString();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    StringBuilder builder = new StringBuilder(dateFormat.format(new Date()));
                    builder.append("/");
                    String[] strings = paths.split("-");
                    for (int i = 0; i < strings.length - 1; i++) {
                        builder.append(strings[i]).append("/");
                    }
                    realPath += File.separator + builder.toString();
                    File file = new File(realPath);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    String fileCurName = strings[strings.length - 1] + suffix;
                    File file1 = new File(file, fileCurName);
                    try {
                        multipartFile.transferTo(file1);
                        article.setNewsImgUrl("/upload/" + builder.toString() + "/" + fileCurName);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                        responseMessage.setSuccess(false).setCode(113).setMessage("文件上传失败");
                    }
                } else {
                    responseMessage.setSuccess(false).setCode(112).setMessage("文件格式不正确");
                }
            } else {
                responseMessage.setSuccess(false).setCode(114).setMessage("文件大小超过限制");
            }
        }/* else {
            if (multipartFile.getSize() == 0) {
                responseMessage.setSuccess(false).setCode(111).setMessage("文件内容为空");
            }
        }*/
        if (responseMessage.getMessage() == null) {//修改部分
            //添加部分
            article.setNewsId(UUID.randomUUID().toString());
            boolean flag = articleMapper.insertSelective(article);
            if (flag) {
                responseMessage.setSuccess(true).setCode(200).setMessage("添加成功");
            } else {
                responseMessage.setSuccess(false).setCode(150).setMessage("添加失败");
            }
        }

        if (responseMessage.getMessage() == null) {//修改部分
            //添加部分
            article.setNewsId(UUID.randomUUID().toString());
            boolean flag = articleMapper.updateByPrimaryKeySelective(article);
            if (flag) {
                responseMessage.setSuccess(true).setCode(200).setMessage("修改成功");
            } else {
                responseMessage.setSuccess(false).setCode(150).setMessage("添加失败");
            }
        }
        return responseMessage;
    }

    @Override
    public Article getOneById(String newsId) {
        return articleMapper.selectByPrimaryKey(newsId);
    }

    @Override
    public Boolean deleteOneById(String newsId) {
        System.out.println("newsId2 = " + newsId);
        return articleMapper.deleteOneById(newsId);
    }

    @Override
    public long batchDelete(String newsIds) {
        System.out.println("newsId = " + newsIds);
        return articleMapper.batchDelete(newsIds);
    }
}